<template>
	<view>
		<!-- 自定义头部导航 -->
		<page-meta>
			<navigation-bar :title="'原材料收货'" front-color="#000000" background-color="#FBFBFF" fixed />
		</page-meta>
		<view class="page">
			<view class="upload-scan">
				<scan-bar id="scanInput" ref="scanBarRef" placeholder="扫描包装条码" @code-scanned="getScanResult"
					:key="datekey"></scan-bar>
			</view>
			<view class="area-header">
				<u-text text="收货信息">
				</u-text>
				<u-text :text="'已收 ' + scanCnt + '/' + receiveInfo.asnPackingDetailList.length" align="right">
				</u-text>
			</view>
			<view class="list-content">
				<!-- 列表 -->
				<u-list class="list" v-if="receiveInfo.asnPackingDetailList && receiveInfo.asnPackingDetailList.length > 0"
					:height="ListViewHeight">
					<u-list-item v-for="(detail, index) in receiveInfo.asnPackingDetailList" :key="detail.materialId">
						<view class="card">
							<view class="right">
								<view class="title">
									包装编码: {{detail.unitBarcode}}
								</view>
								<view class="body">
									物料: {{ detail.materialCode }}({{ detail.materialName }})
								</view>
								<view class="body">
									重量/数量: {{ detail.quantity }}&nbsp;{{ detail.quantityUnit }}
								</view>
								<view class="body">
									供应批次: {{detail.supplyBatch}}
								</view>
							</view>
							<view class="left" v-if="detail.isreceive === 1">
								<u-icon name="checkmark-circle-fill" color="#42b20e" size="26px"></u-icon>
							</view>
						</view>
						<u-line></u-line>
					</u-list-item>
				</u-list>
				<!-- 空数据 -->
				<u-list v-else :height="ListViewHeight">
					<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
				</u-list>
			</view>
			<u-line></u-line>
			<view class="search">
				<u-button type="primary" @click="confirmReceiveData" class="custom-style"
					:loading="posting">确定收货</u-button>
			</view>
		</view>
		<u-modal :show="messageShow" :closeOnClickOverlay="false" title="提示" content="存在未扫描的包装，是否确认收货"
			:showCancelButton="true" @confirm="messageConfirm" @cancel="messageCancel"></u-modal>
	</view>
</template>

<script setup lang="ts">
	import { computed, ref } from 'vue';
	import ScanBar from '@/components/scan-bar/index.vue';
	import { onShow } from '@dcloudio/uni-app';
	import { http } from "../../../utils/http";
	import { useWmsStore } from '@/stores/useWmsStore';

	// 重置表单信息 & 光标自动blur
	const scanBarRef = ref();
	const datekey = ref(new Date());
	const scanCnt = ref(0);
	const posting = ref(false);
	const ListViewHeight = ref(0);
	const messageShow = ref(false);
	
	const receiveDataBack = {
		asnDTO: {},
		asnMaterialDetailList: [],
		asnPackingDetailList: [],
	};

	const receiveInfo = computed(() => JSON.parse(useWmsStore().receiveInfo));

	onShow(async () => {
		ListViewHeight.value = uni.getSystemInfoSync().windowHeight - 160;
		console.log('ListViewHeight.value', ListViewHeight.value);
	})
	// 获取扫码结果
	function getScanResult(result) {
		console.log('扫码结果', result);
		const unitBarcode = result;
		let hasBarCode = false;
		scanCnt.value = 0;
		// 在 details 数组中查找对应的数据
		for (let i = 0; i < receiveInfo.value.asnPackingDetailList.length; i++) {
			if (unitBarcode == receiveInfo.value.asnPackingDetailList[i].unitBarcode) {
				receiveInfo.value.asnPackingDetailList[i].isreceive = 1;
				hasBarCode = true;
				useWmsStore().setReceiveInfo(receiveInfo.value);
			}
			if (receiveInfo.value.asnPackingDetailList[i].isreceive == 1) {
				scanCnt.value++;
			}
		}
		scanBarRef.value.reset();
		if (!hasBarCode) {
			uni.$u.toast('未找到对应包装！');
		}
	}

	async function confirmReceiveData() {
		if (scanCnt.value < receiveInfo.value.asnPackingDetailList.length) {
			messageShow.value = true;
		} else {
			const data = await http({
				url: '/wms/rmreceipt/receipt/',
				method: 'POST',
				data: receiveInfo.value
			});
			
			if(data.code == '200') {
				uni.$u.toast('收货成功！');
				uni.navigateBack({
				    delta: 1, //返回层数，2则上上页
				    success() {
						console.log("success");
						useWmsStore().setReceiveInfo(receiveDataBack);
				    },
				})
			}	
		}
	}

	async function messageConfirm() {
		messageShow.value = false;
		const data = await http({
			url: '/wms/rmreceipt/receipt/',
			method: 'POST',
			data: receiveInfo.value
		}).finally(
			scanBarRef.value.reset()
		);

		if(data.code == '200') {
			uni.$u.toast('收货成功！');
			uni.navigateBack({
				delta: 1, //返回层数，2则上上页
				success() {
					console.log("success");
					useWmsStore().setReceiveInfo(receiveDataBack);
				},
			})
		}
	}
	
	function messageCancel() {
		messageShow.value = false;
	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		flex-direction: column;
	}

	.upload-scan {
		padding: 10px 40rpx 10px 40rpx;
	}

	.area-header {
		color: #909193;
		padding: 10px 15px;
		margin-bottom: 0;
		font-size: 15px;
		background-color: #f3f4f6;
		display: flex;
	}

	.list-content {
		padding: $uni-page-padding;

		.list {
			.card {
				margin-top: 5px;
				margin-bottom: 10px;
				border-radius: 7px;
				display: flex;
				align-items: center;
				padding: 10px 0;
				transition: all 300ms ease;
				box-shadow: 0 0 1px rgba(0, 0, 0, 0.1);

				.left {
					// flex-basis: 90rpx;
					padding-right: 10px;
					text-align: right;
					font-size: 13px;

					.enter {
						line-height: 20px;
						margin-bottom: 5px;
					}

					.number {
						font-size: 13px;
						line-height: 18px;
					}

					:deep(.u-icon--right) {
						flex-direction: row-reverse;
					}
				}

				.right {
					flex: 1;
					padding-left: 15px;

					.title {
						font-size: 18px;
						line-height: 20px;
						margin-bottom: 5px;
						font-weight: 700;
					}

					.body {
						font-size: 14px;
						line-height: 18px;
					}
				}
			}
		}
	}

	.search {
		display: flex;
		flex: 1;

		.custom-style {
			// width: 120px;
			margin: 12px 5px 5px;
			height: 40px;
			border-radius: 8px;

			&:nth-child(1) {
				flex: 1
			}

			&:nth-child(2) {
				flex: 3
			}
		}
	}
</style>